using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;

namespace AppExtensions.Views
{
    public class SessionDictionary : NameObjectCollectionBase, IDictionary<string, object>
    {
        // Creates an empty collection.
        public SessionDictionary()
        {
        }

        // Adds elements from an IDictionary into the new collection.
        public SessionDictionary(IDictionary d, bool bReadOnly)
        {
            foreach (DictionaryEntry de in d)
            {
                this.BaseAdd((string)de.Key, de.Value);
            }
            base.IsReadOnly = bReadOnly;
        }

        public void Add(KeyValuePair<string, object> item)
        {
            this.BaseAdd(item.Key, item.Value);
        }

        public void Clear()
        {
            this.BaseClear();
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            if (base.BaseGet(item.Key) == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public new bool IsReadOnly
        {
            get { return base.IsReadOnly; }
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            
        }

        public override int Count
        {
            get { return base.Count; }
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            if (base.BaseGet(item.Key) != null)
            {
                base.BaseRemove(item.Key);
                return true;
            }
            else
                return false;
        }

        public void Add(string key, object value)
        {
            base.BaseAdd(key, value);
        }

        public bool ContainsKey(string key)
        {
            if (base.BaseGet(key) == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public ICollection<string> Keys
        {
            get { return new List<string>( base.BaseGetAllKeys()); }
        }

        public bool Remove(string key)
        {
            if (base.BaseGet(key) == null)
            {
                return false;
            }
            else
            {
                base.BaseRemove(key);
                return true;
            }
        }

        public bool TryGetValue(string key, out object value)
        {
            value = base.BaseGet(key);
            if (value == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public ICollection<object> Values
        {
            get { return new List<object>(base.BaseGetAllValues()); }
        }

        public new IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>();
            foreach (string key in base.BaseGetAllKeys())
            {
                list.Add(new KeyValuePair<string, object>(key, base.BaseGet(key)));
            }
            return list.GetEnumerator();
        }

        #region IDictionary<string,object> Members


        public object this[string key]
        {
            get
            {
                return base.BaseGet(key);
            }
            set
            {
                base.BaseSet(key, value);
            }
        }

        #endregion
    }

}
